<?php
namespace App\Repositories\Admin;

use App\Jobs\Fun;
use App\Models\Admin\OrderBaleModel;
use App\Models\User\UserModel;
use App\Repositories\BaseRepository;

class OrderBaleRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new OrderBaleModel();
    }

    /**
     * 操作分页获取
     */
    public function getAdminIndex()
    {
        $models = $this->repository
            ->orderBy('id','desc')
            ->paginate(10);
        $models->limit = 10;
        return $models;
    }

    /**
     * 获取web首页货单
     */
    public function getWebIndex()
    {
        return $this->repository
            ->where('status','<',3)
            ->skip(0)
            ->take(4)
            ->orderBy('id','desc')
            ->get();
    }

    public function getIndexByFacStatus($uid=0)
    {
        $query = $this->repository->where('status','<',3);
        if ($uid) {
            $query = $query->where('uid',$uid);
        }
        return $query->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 通过公司ID，获取
     */
    public function getIndexByUid($uid)
    {
        return $this->repository->where('uid',$uid)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 通过公司ID，APP抢单列表获取
     */
    public function getIndexByCid($uid,$cateId)
    {
        $query = $this->repository->where('uid',$uid);
        if ($cateId) { $query = $query->where('cate',$cateId); }
        return $query->where('status','<',3)
            ->where('period','>',time())
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 通过公司ID，APP抢单列表获取
     */
    public function getOrderBaleByCid($cateId,$offset,$pageSize)
    {
        return $this->repository
            ->select('id','name','img','is_rob','period')
            ->where('period','>',time())
            ->where('status','=',OrderBaleModel::status_kaishi)
            ->where('cate',$cateId)
            ->where('del',0)
            ->orderBy('id','DESC')
            ->skip($offset*$pageSize)
            ->take($pageSize)
            ->get();
    }

    /**
     * APP通过CID，获取某个企业货单
     */
    public function getAppBalesByCid($uid,$page)
    {
       // exit('aaaaaaaaaaaffffffff');
        $models = $this->repository
            ->where('uid',$uid)
            ->where('del',0)
            ->orderBy('id','ASC')
            ->skip($page*10)
            ->take(10)
            ->get();
        $dataArr = array();
       // print_r($models);exit;
        if (!count($models)) { return $dataArr; }
        foreach ($models as $key => $model) {
            //$userinfo=UserModel::getinfo($model->uid);
            $dataArr[$key]['id'] = $model->id;
            $dataArr[$key]['img'] =Fun::pImg($model->img);
            $dataArr[$key]['name'] = $model->name;
            $dataArr[$key]['created_at'] = Fun::date($model->created_at);
            $dataArr[$key]['status'] =OrderBaleModel::getStatus($model->status);
            $dataArr[$key]['period'] =  Fun::date($model->period);
           /* $dataArr[$key]['name'] = $model->name;
            $dataArr[$key]['name'] = $model->name;*/
        }
        return $dataArr;
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    public function getShowOne($id)
    {
        $model = $this->repository->where('id',$id)->first();
        return $model;
    }

    /**
     * 通过公司ID，获取
     */
    public function getShowOneByCid($cid)
    {
        return $this->repository->where('cid',$cid)->first();
    }

    /**
     * 通过分类，分页获取
     */
    public function getCategoryOrders($cid)
    {
        $models = $this->repository->select('name','img')->where('cate',$cid)->where('status','<',3)->orderBy('created_at','desc')->paginate(10);
        return $models;
    }

    /**
     * 通过id,获取详情
     */
    public function getCategoryOrdersDetail($id)
    {
        $models = $this->repository->where('id',$id)->first();
        return $models;
    }

    /**
     * 后台货单详情获取
     */
    public function getAdminShow($id)
    {
        return $this->repository
            ->where('id',$id)
            ->first();
    }

    /**
     * 后台货单详情设置
     */
    public function setAdminById($id,$data)
    {
        return $this->repository
            ->where('id',$id)
            ->update($data);
    }

    /**
     * 设置用户删除
     */
    public function setDel($id)
    {
        return $this->repository
            ->where('id',$id)
            ->where('del',0)
            ->update(['del'=>1]);
    }

    /**
     * 尺寸颜色中的尺寸
     */
    public function getSizelistSizes()
    {
        return array(
            'S', 'M', 'L', 'XL', 'XXL', 'XXXL',
        );
    }

    /**
     * 尺寸颜色中的颜色
     */
    public function getSizelistColors()
    {
        return array(
            '白', '红', '蓝',
        );
    }
}